Linux下git的安装与配置

git是开发中常用的工具,如何安装并配置git?

Windows下安装

https://git-scm.com/download/win 下载适用的git安装包
一路点击next完成安装

Linux下安装

  • 使用包管理工具
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Debian/Ubuntu
# For the latest stable version for your release of Debian/Ubuntu
apt-get install git
# For Ubuntu, this PPA provides the latest stable upstream Git version
add-apt-repository ppa:git-core/ppa # apt update; apt install git
# Fedora
yum install git (up to Fedora 21)
dnf install git (Fedora 22 and later)
# Gentoo
emerge --ask --verbose dev-vcs/git
# Arch Linux
pacman -S git
# openSUSE
zypper install git
# Mageia
urpmi git
# Nix/NixOS
nix-env -i git
# FreeBSD
pkg install git
# Solaris 9/10/11 (OpenCSW)
pkgutil -i git
# Solaris 11 Express
pkg install developer/versioning/git
# OpenBSD
pkg_add git
# Alpine
apk add git
  • 编译源码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 先更新系统
yum update -y
# 2. 安装依赖的包
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
# 3.下载源码并解压缩
# https://mirrors.edge.kernel.org/pub/software/scm/git/
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.xz
unzip v2.9.5.zip
cd git-2.9.5
# 4. 编译安装
make prefix=/usr/local/git all
sudo make prefix=/usr/local/git install
# 5. 创建软链
ls -s /usr/local/git/bin/git /usr/bin/git

安装完的测试

linux中启动shell,windows中启动CMD

1
git --version 查看git版本

配置git

1
2
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"

为GitHub账号添加SSH Keys

以公钥认证方式访问SSH协议的Git服务器时无需输入口令,而且更安全。(访问HTTP协议的Git服务器时,比如提交修改,每次都需要输入口令。)

1
2
3
4
5
6
7
# 创建ssh Key
ssh-keygen -t rsa -C "youremail@xxx.com"
# 复制SSH Key
cat ~/.ssh/id_rsa.pub
# 登录github-> Accounting settings图标-> SSH key-> Add SSH key-> 填写SSH key的名称(可以起一个自己容易区分的),然后将拷贝的~/.ssh/id_rsa.pub文件内容粘帖-> add key”按钮添加
# 测试
ssh -T git@github.com